<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreCartItemRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'product_id' => 'required|exists:products,id',
            'variant_id' => 'nullable|exists:product_variants,id',
            'quantity' => 'required|integer|min:1|max:99',
            'specifications' => 'nullable|array',
            'specifications.*.name' => 'required_with:specifications|string|max:100',
            'specifications.*.value' => 'required_with:specifications|string|max:100',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'product_id.required' => '请选择商品',
            'product_id.exists' => '商品不存在',
            'variant_id.exists' => '商品规格不存在',
            'quantity.required' => '请输入数量',
            'quantity.integer' => '数量必须是整数',
            'quantity.min' => '数量不能少于1',
            'quantity.max' => '数量不能超过99',
            'specifications.array' => '规格信息格式错误',
            'specifications.*.name.required_with' => '规格名称不能为空',
            'specifications.*.value.required_with' => '规格值不能为空',
        ];
    }
}
